<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <el-form-item label="项目名称">
          <el-input v-model="queryParam.qry_projectname" placeholder="项目名称"> </el-input>
        </el-form-item>
        <el-form-item label="策划书名称">
          <el-input v-model="queryParam.qry_proposalname" placeholder="名称关键字"> </el-input>
        </el-form-item>
        <el-form-item label="策划书编号">
          <el-input v-model="queryParam.qry_proposalcode" placeholder="策划书编号"> </el-input>
        </el-form-item>
        <el-form-item label="系统部支持">
          <el-select v-model="queryParam.qry_needsupport" placeholder="请选择" filterable collapse-tags clearable>
            <el-option v-for="item in codeModel.needsupportList" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
      </template>
    </tl-list-search>
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a
            >项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName" @expand-change="expandChange" :expand-row-keys="expands">
        <el-table-column type="expand">
          <template slot-scope="props">
            <tl-customer-task ref="customerTaskForm" :customerTaskList="props.row.customertask" :projectInfo="{ projectcode: props.row.projectcode }" :canWatch="canWatch"></tl-customer-task>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="50px" align="center"></el-table-column>
        <el-table-column prop="deptname" label="所属部门" width="95px" sortable="custom" sort-by="deptname" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectcode" label="项目编号" width="150px" sortable="custom" sort-by="projectcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectname" label="项目名称" min-width="300px" sortable="custom" sort-by="projectname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="projectmanager" label="项目经理" width="80px" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="target" label="项目目标" width="200px" align="center" header-align="left" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="proposalcode" label="策划书编号" width="125px" sortable="custom" sort-by="proposalcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="proposalname" label="策划书名称" width="300px" sortable="custom" sort-by="proposalname" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="plandescription" label="策划书描述" width="300px" sortable="custom" sort-by="plandescription" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="version" label="版本" width="55px" sortable="custom" sort-by="version" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="submitdate" label="提交时间" width="155px" sortable="custom" sort-by="submitdate" align="center" header-align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
        <!-- <el-table-column prop="dispstate" label="状态" width="100px" sortable="custom" sort-by="state" align="center" header-align="center" :show-overflow-tooltip="true">
          <template v-slot="scope">
            <el-tag :type="changeStateType(scope.row.state)">
              {{ scope.row.dispstate }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="latestphase" label="最新阶段" width="150px" sortable="custom" sort-by="latestphase" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column> -->
        <el-table-column label="操作" width="100px" v-slot="scope" fixed="right">
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-view" @click.native="checkIsApproval(scope.row.id)">查看 </el-dropdown-item>
              <el-dropdown-item v-if="whetherpmo && (scope.row.whethertop === null || scope.row.whethertop !== '1')" icon="el-icon-circle-plus-outline" @click.native="changeTop(scope.row.projectcode, '0')">置顶</el-dropdown-item>
              <el-dropdown-item v-if="whetherpmo && (scope.row.whethertop === '1')" icon="el-icon-remove-outline" @click.native="changeTop(scope.row.projectcode, '1')">取消置顶</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tl-edit ref="editForm" @refresh="doRefresh" :projectinfo="projectinfo" :oldmilepost="oldmilepost" :planstate="planstate"></tl-edit>
      <tl-approval-edit ref="approvalEditForm" @refresh="doRefresh"></tl-approval-edit>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import tlEdit from './edit'
import tlApprovalEdit from '@/views/cbo/cboApprovalNodeIns/approvalOne'
import tlCustomerTask from '../mpmCustomerTask/editForSystem'
import { checkPermission } from '@tapui/utils/permission'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'mpmProjectPlan',
  mixins: [mixin_list],
  components: { tlEdit, tlApprovalEdit, tlCustomerTask },
  data() {
    return {
      description: '项目策划-列表查询',
      urlPrefix: '/mpm/projectPlan/',
      url: {
        queryCodeList: '/mpm/projectPlan/dataCodeList',
        queryTaskForSystem: '/mpm/projectTask/queryTaskForSystem',
        checkIsApproval: '/mpm/projectPlan/checkIsApproval',
        list: '/mpm/projectPlan/listForOpen',
        queryCanWatch: '/mpm/project/queryCanWatch',
        changeTop: 'mpm/project/changeTop'
      },
      // 排序参数
      sorterInfo: {
        sidx: "NVL(whethertop,0) desc, NVL(toptime,to_date('2000-01-01', 'yyyy-mm-dd')) desc, createon",
        sord: 'desc'
      },
      oldmilepost: [],
      planstate: '',
      nodeinsid: '',
      projectinfo: {},
      whetherpmo: false,
      canWatch: false,
      // 要展开的行
      expands: []
    }
  },
  methods: {
    expandChange(row, expandedRows) {
      debugger
      getAction(this.url.queryTaskForSystem, { projectcode: row.projectcode, needsupport: this.queryParam.qry_needsupport })
        .then((res) => {
          if (res.success) {
            debugger
            let indexRow = row.rowno - (this.pageInfo.currentPage - 1) * this.pageInfo.pageSize - 1
            this.$set(this.datas[indexRow], 'customertask', res.result)
          }
        })
        .catch((err) => {
          this.$tap.alert('加载任务信息出错，请联系管理员！')
        })
    },
    doChangeRowClassName({ row, rowIndex }) {
      if (row.disabledexpand) {
        return 'row-expand-cover'
      }
    },
    changeTop(projectcode, type) {
      putAction(this.url.changeTop, { projectcode: projectcode, type: type, sourcetype: 'MpmProjectPlan' }).then((res) => {
        if (res.success) {
          this.$tap.message(res.message, 'success')
        } else {
          this.$tap.alert(res.message, 'warning')
        }
        this.doRefresh()
      })
    },
    checkIsApproval(id) {
      let param = {
        sourceid: id,
        sourcetype: 'MpmProjectPlan'
      }
      getAction(this.url.checkIsApproval, param).then((res) => {
        if (res.success) {
          if (res.result.isapprover) {
            this.$refs.approvalEditForm.nodeInsId = res.result.nodeinsid
            this.openViewDialog('approvalEditForm', id)
          } else {
            this.openViewDialog('editForm', id)
          }
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    },
    queryCanWatch() {
      getAction(this.url.queryCanWatch).then(res => {
        if (res.success) {
          this.whetherpmo = res.result.whetherpmo
        } else {
          this.whetherpmo = false
        }
      })
    },
    /** 打开详情对话框实现*/
    openViewDialog(editForm, id) {
      getAction('/mpm/projectPlan/load?', { id: id }).then((res1) => {
        if (res1.success) {
          this.planstate = res1.result.state
          // 查询projectinfo
          getAction('/mpm/projectpolymerization/list', { qry_projectcode: res1.result.projectcode }).then((res2) => {
            if (res2.success) {
              this.projectinfo = res2.result.dataList[0]
              if (!this.beforeOpenViewDialog(editForm, id)) {
                return
              }
              this.$refs[editForm].id = id
              this.$refs[editForm].isAdd = false
              this.$refs[editForm].visible = true
              this.$refs[editForm].disableEditSubmit = true
              this.$nextTick(() => {
                this.afterOpenViewDialog(editForm, id)
              })
              let that = this
              setTimeout(() => {
                that.clickRowkey = id
              }, 100)
            } else {
              this.$tap.alert(res2.message, 'warning')
            }
          })
        } else {
          this.$tap.alert(res1.message, 'warning')
        }
      })
    },
    beforeLoadData() {
      this.queryParam.qry_open = 1
      this.queryParam.opensid = '1'
      return true
    },
    beforeOpenAddDialog() {
      let isHaveApprovalingDatas = false
      this.datas.forEach((element) => {
        if (element.state === 1 || element.state === 0 || element.state === 2) {
          isHaveApprovalingDatas = true
        }
      })
      if (isHaveApprovalingDatas) {
        this.$tap.confirm('存在开立或者审批中的单据，无法新增单据').then((_) => {
          return false
        })
      } else {
        return true
      }
    },
    afterLoadData() {
      this.expands = []
      let planid = ''
      for (let i = 0; i < this.datas.length; i++) {
        if (this.datas[i].state === 9 && this.datas[i].enabled === 1) {
          planid = this.datas[i].id
          break
        }
      }
      if (planid !== '') {
        var params = {
          qry_planid: planid
        }
        getAction('mpm/projectMilepost/list', params).then((res) => {
          if (res.success) {
            this.oldmilepost = res.result.dataList
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      }
    },
    /** 表格双击事件 */
    doRowDbClick(row, column, event) {
      this.openViewDialog('editForm', row[this.$refs.mainTable.rowKey])
    },
    /** 改变审核状态标签颜色 */
    changeStateType(state) {
      return state === 0 ? 'info' : state === 2 ? 'danger' : state === 9 ? 'success' : ''
    }
  },
  created() {
    this.queryCanWatch()
  }
}
</script>

<style scoped>
::v-deep .el-table .row-expand-cover .cell .el-table__expand-icon {
  display: none;
}</style>
